import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App'
import router from './router'
import axios from 'axios';
import QS from 'qs'
import "@babel/polyfill";
import { parseUrlParams } from "./utils/query";
import 'vant/lib/index.css';
import { login } from "./constants/api";
import {
    Button,
    Cell, CellGroup,
    Field,
    DatetimePicker,
    Popup,
    Checkbox,
    Icon,
    Picker,
    Notify,
    Radio, RadioGroup,
    Toast,
    Image,
} from 'vant';

Vue.use(Button);
Vue.use(Cell).use(CellGroup);
Vue.use(Field);
Vue.use(DatetimePicker);
Vue.use(Popup);
Vue.use(Checkbox);
Vue.use(Icon);
Vue.use(Picker);
Vue.use(Notify);
Vue.use(RadioGroup);
Vue.use(Radio);
Vue.use(Toast);
Vue.use(Image);

Vue.config.productionTip = false
Vue.prototype.$axios = axios;
Vue.prototype.qs = QS;
axios.defaults.withCredentials = false;

function intercept() {
    router.beforeEach( async (to, from, next) => {
        const webUrl = window.location.href;
        if (webUrl.indexOf('openId=') !== -1) {
            const openId = parseUrlParams("openId", webUrl);
            const json = await axios({
                method: "POST",
                url: login,
                params: {
                    openId
               }
            })
            if (json.data.code === 200) {
                const data = json.data.data.bussData;
                localStorage.setItem('sessionId', data.sessionid);
                localStorage.setItem('userInfo', JSON.stringify(data));
                next();
            } else {
                Notify({ type: 'warning', message: json.data.data.errMsg });
            }
        } else {
            next()
        }
    });
}


//樊嵩渊微信 token
// localStorage.setItem('token', "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJva21Ea3Z2Q1R1Rlo5UXBicDRXMUxVMW5ZeEVFIiwicm9sZSI6WyJVU0VSIiwiUEVSU09OQUwiXSwiY3JlYXRlZCI6MTU3MzExNzg3MTM5OSwiZXhwIjoxNTczNzIyNjcxfQ.I-ODzLb5iecJHqySUvfTXVJpzzQA50BxBbUYOjNkwnxqltBMhslTDkPnyEH2irxmGPFIPo5Kpm0qGhYC_jsXTQ")
// localStorage.setItem('userAuthInfo', JSON.stringify({"id":56,"userType":"PERSONAL","relationId":7}))
intercept()

router.afterEach((to,from,next) => {
    window.scrollTo(0,0);
})

new Vue({
    router,
    render: h => h(App)
}).$mount('#app-box')